package weaver.interfaces.jjy.duss.yy;

import com.sap.mw.jco.JCO;
import weaver.interfaces.jjy.duss.util.SAPConnPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-01-07
 * @Description: 档期选品单审批结果回传是SAP
 * @ModifyLog:
 **/

public class Z_IF_OPS009 {
    public Map execute(Map<String, String> map, List<Map<String, String>> mapList) {

        SAPConnPool SAPConn = new SAPConnPool();
        JCO.Client myConnection = SAPConn.getConnection();
        JCO.Function bapi = SAPConn.excuteBapi("Z_IF_OPS009");

        JCO.ParameterList input = bapi.getImportParameterList();//输入参数和结构处理
        JCO.ParameterList inputtable = bapi.getTableParameterList();//输入表的处理

        JCO.Table t1 = inputtable.getTable("IT_ITAB");
            t1.appendRow();
            t1.setValue(map.get("DQXPNO"), "DQXPNO");       //选品单号
            t1.setValue(map.get("SJDQNO"), "SJDQNO");       //实际档期编码
            t1.setValue(map.get("REL_FLAG"), "REL_FLAG");   //审批标记
            t1.setValue(map.get("REL_USER"), "REL_USER");   //审批人
            t1.setValue(map.get("REL_DATUM"), "REL_DATUM"); //审批日期
            t1.setValue(map.get("REL_UZEIT"), "REL_UZEIT"); //审批时间

        JCO.Table t2 = inputtable.getTable("IT_ITAB1");
        for (Map<String, String> map1 :mapList) {
            t2.appendRow();
            t2.setValue(map1.get("DQXPNO"), "DQXPNO");     //选品单号
            t2.setValue(map1.get("DQXPLN"), "DQXPLN");     //选品序号
            t2.setValue(map1.get("MATNR"), "MATNR");       //商品编码
            t2.setValue(map1.get("SPBZ"), "SPBZ");         //审批备注
        }
        //执行函数
        myConnection.execute(bapi);
        JCO.ParameterList out = bapi.getExportParameterList();//输出参数和结构处理
        JCO.ParameterList Table00 = bapi.getTableParameterList();//输出表的处理

        //返回处理结果以及日志
        JCO.Structure stu1 = out.getStructure("ES_MSG");
        Map<String, String> mapreturn = new HashMap<>();
        String flag = (String) stu1.getValue("TYPE");
        String message = (String) stu1.getValue("MESSAGE");
        mapreturn.put("flag",flag);
        mapreturn.put("message",message);
        System.out.println("返回结果："+ mapreturn);
        return mapreturn;
    }

    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        map.put("DQXPNO","1000000009");
        map.put("SJDQNO","1000000011");
        map.put("REL_FLAG","4");
        map.put("REL_USER","WANGTING");
        map.put("REL_DATUM","20200110");
        map.put("REL_UZEIT","154248");
        List<Map<String, String>> list = new ArrayList<>();
        Map<String,String> map1 = new HashMap<>();
         for(int i =0;i<5;i++){
             map1.put("DQXPNO","1000000009");
             map1.put("DQXPLN","000010");
             map1.put("MATNR","51000007");
             map1.put("SPBZ","123");
             list.add(map1);
         }
        Z_IF_OPS009 zIfOps009 = new Z_IF_OPS009();

        zIfOps009.execute(map,list);

    }
}
